#!/usr/bin/python3
# -*- coding:UTF-8 -*-

import AutoExecUtils
import os
import traceback
import argparse
import json
from bson.json_util import dumps, loads


def usage():
    pname = os.path.basename(__file__)
    print(pname + " --outputfile <path> ")
    exit(1)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--objtypes", default="{}", help="Object types to import(json)")

    args = parser.parse_args()
    objTypeMap = json.loads(args.objtypes)

    hasError = 0
    for objTypeItem in objTypeMap.keys():
        (objCat, objType) = objTypeItem.split("/")
        try:
            AutoExecUtils.callNativeApi(
                "/neatlogic/api/rest/cmdb/sync/object/save",
                {"objCategory": objCat, "objType": objType},
            )
        except Exception as err:
            hasError = 1
            print("WARN: Import obj type:{}:{} failed, {}".format(objCat, objType, err))
